

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * Created by lenovo on 2018/4/1.
 */
public class Home extends JFrame implements ActionListener {
    //定义组件
    JPanel menu, show;//面板
    JLabel jlb1, jlb2;//标签
    JButton jb1, jb2, jb3, jb4,jb5,jb6;//按钮
    JTextField jtf;//文本
    ArrayList<Jade> jades;
    JScrollPane jsb;
    private User s;

    public static void main(String[] args) {
        Home home = new Home(null);
    }

    public Home(User u) {
        s = u;
        jades = ExInform.show();
        //菜单
        menu = new JPanel();
        menu.setBounds(0, 0, 1024, 40);
        try {
            //设置外观
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //显示内容
        show = new JPanel();
        show.setLayout(new FlowLayout());//设置内容显示布局
        int h;
        if(jades.size()%4==0){
            h = jades.size()/4;
        }else {
            h = jades.size()/4+1;
        }
        show.setPreferredSize(new Dimension(970,h*345));

        //创建搜索框
        jtf = new JTextField(20);
        jb1 = new JButton("搜索");
        jb1.addActionListener(this);//为搜索按钮添加监听

        jlb1 = new JLabel("                                       ");
        jb2 = new JButton("用户登录");
        jb2.addActionListener(this);

        menu.add(jtf);
        menu.add(jb1);
        menu.add(jlb1);
        menu.add(jb2);

        //获取显示数据列表
        for (int i = 0; i < jades.size(); i++) {
            JPanel myPanel;
            if(i<jades.size())
                myPanel = new MyPanel(jades.get(i),this);
            else
                myPanel = new JPanel();
            if(i%2 == 0)
                myPanel.setBackground(Color.pink);
            else
                myPanel.setBackground(Color.CYAN);
            myPanel.setPreferredSize(new Dimension(1024/5,340));
            show.add(myPanel);
        }

        //判断用户登录
        if (u != null) {
            menu.remove(jb2);
            jlb2 = new JLabel("当前用户：  " + u.getNC() + "    ");
            jb3 = new JButton("注销");
            jb4 = new JButton("上架物品");
            jb5 = new JButton("我的物品");
            jb6 = new JButton("资金查询");
            jb3.addActionListener(this);
            jb4.addActionListener(this);
            jb5.addActionListener(this);
            jb6.addActionListener(this);
            //加载登录组件
            menu.add(jlb2);
            menu.add(jb3);
            menu.add(jb4);
            menu.add(jb5);
            menu.add(jb6);
        }

        jsb = new JScrollPane(show);
        jsb.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        //添加分栏
        JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, menu, jsb);
        sp.setLastDividerLocation(40);

        this.setContentPane(sp);


        //设置窗体
        this.setTitle("主界面");//窗体标签
        this.setSize(1024, 780);//窗体大小
        this.setLocationRelativeTo(null);//在屏幕中间显示(居中显示)
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出关闭JFrame
        this.setVisible(true);//显示窗体


        //锁定窗体
        this.setResizable(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand() == "用户登录") {
            new Login();
            this.dispose();
        }
        if (e.getActionCommand() == "上架物品") {
            new Sell();
            this.dispose();
        }
        if (e.getActionCommand() == "注销") {
            ExInform.setU(null);
            new Home(null);
            this.dispose();
        }
        if(e.getActionCommand() == "我的物品"){
            new MyJade(ExInform.getU(),ExInform.show());
            this.dispose();
        }
        if(e.getActionCommand() == "资金查询"){
            new Funds(s);
            this.dispose();
        }
    }
}


